package gbench.appdemo.trading.position;

import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

import gbench.appdemo.trading.agent.Agent;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * 交易头寸单
 * 头寸单:用户的下单方式
 * 空单就是卖单：
 * 多单就是卖单
 * @author gbench
 *
 */
@Data @AllArgsConstructor @NoArgsConstructor
public class TradingPositionBill {
    private String id = UUID.randomUUID().toString();
    private TradeItem underlying;// 产品
    private Double quantity;// 数量
    private Double unitPrice;// 单价
    private Double amount;// 金额
    private String unitName="手";// 单位
    private TradingPostion position;// 头寸位置
    private Agent owner;// 头寸持有者
    private LocalDateTime createTime = LocalDateTime.now();// 创建时间
    private long ttl = 5*60*1000; // 失效时间
    public LocalDateTime getDueTime() {// 截止时间
        return createTime.plus(ttl, ChronoUnit.MILLIS);
    }
    
    public String toString() {
        return MessageFormat.format("id:{0},pos:{1},udl:{2},owner:{3},qty:{4,number,#.##},price:{5,number,#.##},date:{6}", 
            this.id, this.position,this.underlying.getName(),this.owner.getName(),
            this.getQuantity(),this.unitPrice,this.getCreateTime().format(DateTimeFormatter.ISO_LOCAL_TIME));
    }
    
    /**
     * 透视信息是否有效
     * @return
     */
    public boolean isAvailable() {
        return LocalDateTime.now().isBefore(this.getDueTime());
    }
    
    public enum TradingPostion{SHORT,LONG}// 买卖头寸
}
